package com.linc.multithread;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.linc.multithread.logic.PiMaker;

import java.math.BigDecimal;

public class JoinMethodActivity extends AppCompatActivity {

    private TextView tvShow;
    private static BigDecimal result;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_method);
        tvShow = (TextView) findViewById(R.id.tv_show);

    }

    public void doCompute(View view) {
        //lambda expressions
        Runnable runnable = () -> {
            result = PiMaker.computePi(50000);
        };

        Log.e("linc","show text");
        showProgress();
        Thread thread = new Thread(runnable);
        thread.start();

        try {
            thread.join();
        }
        catch (InterruptedException ex) {
            //Should never arrive here because interrupt() is never called.
        }

        dismissProgress();
        tvShow.setText(result+"");
    }

    private void showProgress() {
        if(progressDialog == null) {
            progressDialog = new ProgressDialog(this);
//            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        progressDialog.show();
    }

    private void dismissProgress() {
        if(progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}
